package com.loveprogrammer.engine.battle;

import com.loveprogrammer.engine.AttackResult;
import com.loveprogrammer.model.Character;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GroupAttack
 * @Description 我要打十个
 * @Author admin
 * @Date 2024/2/20 17:43
 * @Version 1.0
 */
public class GroupAttack extends Attack {

    private Character attack;

    private List<? extends Character> defenceList;

    public GroupAttack(Character character, List<? extends Character> defenceList) {
        this.attack = character;
        this.defenceList = defenceList;
        setIntervalTime(computeInterval(character.getSpeed()));
        setSpeed(character.getSpeed());
    }

    @Override
    public AttackResult run() {
        // 自己血量少于0 返回
        if(attack.getCurrentHp() > 0) {        // 遍历并找到血量最少的攻击
            defenceList = defenceList.stream().filter(e -> e.getCurrentHp() > 0).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(defenceList)) {
                AttackResult result = attack.attack(defenceList);
                return result;
            }
        }
        return null;
    }

    @Override
    public boolean checkContinue() {
        return attack.getCurrentHp() > 0 && defenceList.stream().anyMatch(e -> e.getCurrentHp() > 0);
    }

}
